<template>
  <div id="logintwo">
    <form @submit.prevent="submitForm" class="login-form">
      <h1>{{ isLogin ? 'Log In' : 'Register' }}</h1>
      <!-- 其他登录表单字段... -->
      <div class="txtb" v-if="isLogin">
        <input type="text" v-model="username" @input="onInput('username')">
        <span :class="{ 'focus': username !== '' }" data-placeholder="Username"></span>
      </div>
      <div class="txtb" v-if="isLogin">
        <input type="password" v-model="password" @input="onInput('password')">
        <span :class="{ 'focus': password !== '' }" data-placeholder="Password"></span>
      </div>

      <!-- 使用 v-if 控制显示登录或注册按钮 -->
      <div class="txtb" v-if="!isLogin">
        <input type="text" v-model="username" @input="onInput('username')">
        <span :class="{ 'focus': username !== '' }" data-placeholder="Username"></span>
      </div>
      <div class="txtb" v-if="!isLogin">
        <input type="password" v-model="password" @input="onInput('password')">
        <span :class="{ 'focus': password !== '' }" data-placeholder="Password"></span>
      </div>
      <input type="submit" class="logbtn" :value="isLogin ? 'Log In' : 'Register'">
      <div class="bottom-text">
        {{ isLogin ? 'Don\'t have account?' : 'Already have an account?' }}
        <a href="#" @click.prevent="toggleForm">
          {{ isLogin ? 'Sign up' : 'Log in' }}
        </a>
      </div>

    </form>
  </div>
</template>

<script>
import $axios from "axios";

export default {
  data() {
    return {
      isLogin: true, // 控制登录和注册的切换
      username: '',
      password: '',
      email: '' // 假设的注册邮箱字段
    };
  },
  methods: {
    async submitForm() {
      if (this.isLogin) {
        await this.login()
        // 发送登录请求到 '/api/login'
      } else {
        // 发送注册请求到 '/api/register'
        await this.regist()
      }
    },
    login() {
      const formData = new FormData()
      formData.append('username', this.username)
      formData.append('password', this.password)
      $axios.post('/api/user/login', formData)
          .then(res => {
            console.log(res)
            if (res.data.code === 200) {
              console.log(res)
              this.$router.push({path: 'home'}, () => {
              }, () => {
              });
            }
          })
    },
    regist(){
      const formData = new FormData()
      formData.append('username',this.username)
      formData.append('password',this.password)
      return this.$axios.post('/api/user/regist',formData)
          .then(res =>{
        console.log("1111")
        if (String(res.data.code) === '200') {
          if (res.data.data.code === '200'){
            this.isLogin = true
          }
        }
      })
    },
    onInput(field) {
      if (field === 'username') {
        this.username = this.username.trim();
      } else if (field === 'password') {
        this.password = this.password.trim();
      }
    },
    toggleForm() {
      this.isLogin = !this.isLogin; // 切换登录和注册状态
    }
  }
}
</script>

<style scoped>
#logintwo{
  width: 100%;
  height: 100%;
  background-size: 100% 100%;
  background-image: url("../../../assets/preview.jpg");
  background-repeat: no-repeat;
  background-position: center center;
  overflow: auto;
  position: fixed;
  line-height: 100%;
}
*{
  margin: 0;
  padding: 0;
  text-decoration: none;
  font-family: montserrat;
  box-sizing: border-box;
}

/*body{
  min-height: 100vh;
  background-image: linear-gradient(120deg, #3498db, #8e44ad);
}*/

.login-form{
  width: 360px;
  background: #f1f1f1;
  height: 580px;
  padding: 80px 40px;
  border-radius: 10px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.login-form h1{
  text-align: center;
  margin-bottom: 60px;
}

.txtb{
  border-bottom: 2px solid #adadad;
  position: relative;
  margin: 30px 0;
}

.txtb input{
  font-size: 15px;
  color: #333;
  border: none;
  width: 100%;
  outline: none;
  background: none;
  padding: 0 5px;
  height: 40px;
}

.txtb span::before{
  content: attr(data-placeholder);
  position: absolute;
  top: 50%;
  left: 5px;
  color: #adadad;
  transform: translateY(-50%);
  z-index: -1;
  transition: .5s;
}

.txtb span::after{
  content: '';
  position: absolute;
  left: 0%;
  top: 100%;
  width: 0%;
  height: 2px;
  background: linear-gradient(120deg, #3498db, #8e44ad);
  transition: .5s;
}

.txtb .focus::before {
  top: -5px; /* 或者其他你想要的距离 */
}

.txtb .focus::after {
  width: 100%;
}

.logbtn{
  display: block;
  width: 100%;
  height: 50px;
  border: none;
  background: linear-gradient(120deg, #3498db, #8e44ad, #3498db);
  background-size: 200%;
  color: #fff;
  outline: none;
  cursor: pointer;
  transition: .5s;
}

.logbtn:hover{
  background-position: right;
}

.bottom-text{
  margin-top: 60px;
  text-align: center;
  font-size: 13px;
}

</style>